<?php

namespace app\admin\controller;

use app\common\model\UserProfileModel;
use app\common\model\UserProfitModel;
use Exception;
use app\common\model\OrderModel;
use app\common\model\UserModel;
use think\Db;
use think\facade\Request;

class ChargeController extends BaseController
{
    public function manual()
    {
        return $this->assign(['uid' => Request::param('uid')])->fetch();
    }

    public function manual_post()
    {
        if (!ctype_digit($id = $this->post('id')) || $id <= 0) {
            return static::bulidFail('用户ID非法');
        }

        if (!in_array($type = $this->post('opt_type'), ['0', 1])) {
            return static::bulidFail('操作非法');
        }

        if (!is_numeric($diamond = rtrim(trim($this->post('diamond', '')), '.')) || bccomp($diamond, '0', 2) < 0) {
            return static::bulidFail('请正确输入宝石数量');
        }

        if (!is_numeric($gold = rtrim(trim($this->post('gold', '')), '.')) || bccomp($gold, '0', 2) < 0) {
            return static::bulidFail('请正确输入钻石数量');
        }

        if (!is_numeric($point = rtrim(trim($this->post('point', '')), '.')) || bccomp($point, '0', 2) < 0) {
            return static::bulidFail('请正确输入经验值数量');
        }

        $remark = trim($this->post('remark', ''));
        if (mb_strlen($remark, 'UTF-8') < 2 || mb_strlen($remark, 'UTF-8') > 200) {
            return static::bulidFail('备注理由在2-200个字符之间');
        }

        if (!$user = UserModel::where('id', $id)->field('id, nick_name, gold,diamond, point')->lock()->find()) {
            return static::bulidFail('用户不存在');
        }

        $remarkMsg = '';
        if ($remark) {
            $remarkMsg = ' 备注: ' . $remark;
        }

        $tmpGold    = bcmul($gold, '100', 0);
        $tmpDiamond = bcmul($diamond, '100', 0);
        Db::startTrans();
        try {
            switch ((int)$type) {
                case 0:
                    if ($tmpDiamond > $user->diamond) {
                        return static::bulidFail('用户宝石余额不足');
                    }

                    if ($tmpGold > $user->gold) {
                        return static::bulidFail('用户钻石余额不足');
                    }

                    if ($point > $user->point) {
                        return static::bulidFail('用户经验值不足');
                    }

                    $data = [];
                    if ($diamond && !parent::addAdminLog("扣除用户" . $user->nick_name . "(" . $user->id . ")" . "宝石：" . $diamond . $remarkMsg, $user->id)) {
                        break;
                    } else {
                        if ($tmpDiamond > 0) {
                            $data[] = [
                                'consume_type' => 21,
                                'uid'          => $user->id,
                                'f_uid'        => $this->admin_id,
                                'coin_count'   => $tmpDiamond,
                                'type'         => 0,
                                'coin_type'    => 2,
                                'content'      => "管理员:{$this->admin_name} 手动扣除 {$diamond} 宝石",
                                'remark'       => $remark,
                                'resid'        => 0,
                                'count'        => 0,
                                'order_no'     => createOrderNo(),
                                'liveid'       => 0,
                                'create_time'  => date('Y-m-d H:i:s')
                            ];
                        }
                    }

                    if ($gold && !parent::addAdminLog("扣除用户" . $user->nick_name . "(" . $user->id . ")" . "钻石：" . $gold . $remarkMsg, $user->id)) {
                        break;
                    } else {
                        if ($tmpGold > 0) {
                            $data[] = [
                                'consume_type' => 21,
                                'uid'          => $user->id,
                                'f_uid'        => $this->admin_id,
                                'coin_count'   => $tmpGold,
                                'type'         => 0,
                                'coin_type'    => 1,
                                'content'      => "管理员:{$this->admin_name} 手动扣除 {$gold} 钻石",
                                'remark'       => $remark,
                                'resid'        => 0,
                                'count'        => 0,
                                'order_no'     => createOrderNo(),
                                'liveid'       => 0,
                                'create_time'  => date('Y-m-d H:i:s')
                            ];
                        }
                    }

                    if ($data) {
                        (new UserProfitModel())->saveAll($data);
                    }

                    $user->diamond = $user->diamond - $tmpDiamond;
                    $user->gold    = $user->gold - $tmpGold;
                    $user->point   = $user->point - $point;
                    if ($user->save()) {
                        Db::commit();
                        redis_del('USER:FULLINFO:' . $user->id);
                        return static::bulidSuccess();
                    }
                    break;
                case 1:
                    $user->diamond = $user->diamond + $tmpDiamond;
                    $user->gold    = $user->gold + $tmpGold;
                    $user->point    = $user->point + $point;
                    $data          = [];
                    if ($diamond && !parent::addAdminLog("给用户" . $user->nick_name . "(" . $user->id . ")" . "充值宝石：" . $diamond . $remarkMsg, $user->id)) {
                        break;
                    } else {
                        if ($tmpDiamond > 0) {
                            $data[] = [
                                'consume_type' => 20,
                                'uid'          => $user->id,
                                'f_uid'        => $this->admin_id,
                                'coin_count'   => $tmpDiamond,
                                'type'         => 1,
                                'coin_type'    => 2,
                                'content'      => "管理员:{$this->admin_name} 手动充值 {$diamond} 宝石",
                                'remark'       => $remark,
                                'resid'        => 0,
                                'count'        => 0,
                                'order_no'     => createOrderNo(),
                                'liveid'       => 0,
                                'create_time'  => date('Y-m-d H:i:s')
                            ];
                        }
                    }

                    if ($gold && !parent::addAdminLog("给用户" . $user->nick_name . "(" . $user->id . ")" . "充值钻石：" . $gold . $remarkMsg, $user->id)) {
                        break;
                    } else {
                        if ($tmpGold > 0) {
                            $data[] = [
                                'consume_type' => 20,
                                'uid'          => $user->id,
                                'f_uid'        => $this->admin_id,
                                'coin_count'   => $tmpGold,
                                'type'         => 1,
                                'coin_type'    => 1,
                                'content'      => "管理员:{$this->admin_name} 手动充值 {$gold} 钻石",
                                'remark'       => $remark,
                                'resid'        => 0,
                                'count'        => 0,
                                'order_no'     => createOrderNo(),
                                'liveid'       => 0,
                                'create_time'  => date('Y-m-d H:i:s')
                            ];
                        }
                    }

                    if ($data) {
                        (new UserProfitModel())->saveAll($data);
                    }

                    if ($user->save()) {
                        Db::commit();
                        redis_del('USER:FULLINFO:' . $user->id);
                        return static::bulidSuccess();
                    }
                    break;
            }
            Db::rollback();
            return static::bulidFail();
        } catch (Exception $exception) {
            Db::rollback();
            return static::bulidFail($exception->getMessage());
        }
    }
}
